package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.Blog;
import com.example.demo.service.BlogService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("blog")
@AllArgsConstructor
public class BlogController{
	private BlogService blogService;
	private BladeLogger bladeLogger;

	/**
	 * 新增
	 */
	@PostMapping("/save")
	public R save(@RequestBody Blog blog){
		return R.status(blogService.save(blog));
	}
	/**
	 * 修改
	 */
	@PostMapping("/update")
	public R update(@RequestBody Blog blog){
		return R.status(blogService.updateById(blog));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "新增或修改", notes = "传入notice")
	public R submit(@RequestBody Blog blog) {
		return R.status(blogService.saveOrUpdate(blog));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	public R remove(@RequestParam String ids) {
		return R.status(blogService.removeByIds(Func.toIntList(ids)));
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	public R<Blog> detail(Integer id) {
		Blog detail = blogService.getById(id);
		return R.data(detail);
	}

	/**
	 * 查询多条
	 */
	@GetMapping("/list")
	@ApiLog
	public R<IPage<Blog>> list(@ApiIgnore @RequestParam Map<String, Object> blog, Query query) {
		bladeLogger.debug("blog列表","测试日志");
		IPage<Blog> pages = blogService.page(Condition.getPage(query), Condition.getQueryWrapper(blog, Blog.class));
		return R.data(pages);
	}


	/**
	 * 查询多条单个属性equal查询
	 */
	@GetMapping("/listByBlog")
	public R<List<Blog>> listByBlog(Blog blog) {
		List<Blog> list = blogService.list(Wrappers.query(blog));
		return R.data(list);
	}

	/**
	 * 查询多条，like查询
	 */
	@GetMapping("/listByBlogLike")
	public R<List<Blog>> listByBlogLike(@RequestParam Map<String, Object> blog) {
		List<Blog> list = blogService.list(Condition.getQueryWrapper(blog,Blog.class));
		return R.data(list);
	}


	/**
	 * 查询多条，lamba
	 */
	@GetMapping("/listByBlogLambda")
	public R<List<Blog>> listByBlogLambda(@RequestParam Map<String, Object> blog) {
		List<Blog> list = blogService.list(Condition.getQueryWrapper(blog, Blog.class).lambda().orderByDesc(Blog::getBlogDate));
		return R.data(list);
	}

	/**
	 * 分页
	 */
	@GetMapping("/page")
	public R<IPage<Blog>> page(@ApiIgnore @RequestParam Map<String, Object> blog, Query query) {
		IPage<Blog> pages = blogService.page(Condition.getPage(query), Condition.getQueryWrapper(blog, Blog.class));
		return R.data(pages);
	}

	@GetMapping("/testtest")
	public R<List<Blog>> listest(){
		return R.data(blogService.getBlogs());
	}



}
